import dayjs from "dayjs";
import { useI18n } from "vue-i18n";
import "dayjs/locale/zh-cn";
import { useAppStore } from "@/stores/modules/app";
import { storeToRefs } from "pinia";
dayjs.locale("zh-cn");

export function useLanguage() {
  const appStore = useAppStore();

  // 当前语言
  const { language } = storeToRefs(appStore);

  // i18n实例
  const { locale } = useI18n();

  // locale和language的同步
  locale.value = language.value;

  /**
   * 切换系统语言
   */
  function toggleLanguage() {
    // 更新store
    language.value = appStore.language === "zh-cn" ? "en" : "zh-cn";
    // 更改系统语言
    locale.value = language.value;
  }

  return { language, toggleLanguage };
}
